﻿$(document).ready(function () {

    if ($("#result").html() == "添加成功" || $("#result").html() == "修改成功") {

        //关闭当前对话框并刷新父页面
        $(window.top.document).find("#searchBtn").click();
        window.top.art.dialog.get('operationmanagevehiclecreate').close();
    }

    if ($("#result").html() == "添加成功，发业务中心消息失败" || $("#result").html() == "更改成功，发业务中心消息失败") {
        $(window.top.document).find("#searchBtn").click();
    }

    $("#tt").tabs({
        height: 510,
        width:630,
        onSelect: function () {
        }
    });

    $("#tt").tabs('select', '附件信息');
    $("#tt").tabs('select', '基本信息');

    $("#selectParentUnit").click(function () {
        artDialogForm("/Vehicle/PickParentUnit", "选择所属单位", "operationmanagevehiclecreatepickparentunit", 400, 300);
    });

    $("#selectInstallUnit").click(function () {
        artDialogForm("/Vehicle/PickInstallUnit", "选择安装单位", "operationmanagevehiclecreatepickinstallunit", 400, 300);
    });

    $(window.top.art.dialog.get('operationmanagevehiclecreate').iframe).attr("scrolling", "no");
    //$("#popContainerDiv").height($("body").height());

    //$("#vehiclecreateform").validationEngine('attach', { promptPosition: "centerRight", autoPositionUpdate: true });

    jQuery.validator.addMethod("alternativelength",
        function (value,element, param) {
            return this.optional(element) || value.length == param[0] || value.length == param[1];
        },
        "输入长度不正确"
    );

    var validator = $("#vehiclecreateform").validate({
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        rules: {
            FInnerId:
            {
                required: true,
                maxlength: 50
            },
            VehicleId:
            {
                required: true,
                maxlength: 50
            },
            InstallUnitName:
            {
                required: true,
                maxlength: 50
            },
            InstallDate:
            {
                required: true
            },
            PARTENTUNITNAME:
            {
                required: true,
                maxlength: 50
            },
            SIM:
            {
                required: true,
                maxlength: 50
            },
            VehicleType:
            {
                required: true,
                maxlength: 50
            },
            GPSType:
            {
                required: true,
                digits: true //整数
            },
            GPSVersion:
            {
                digits: true //整数
            },
            TerminalVersion:
            {
                digits: true //整数
            },
            InstallStaffId:
            {
                maxlength: 50
            },
            VehicleColor:
            {
                maxlength: 50
            },
            GPSID:
            {
                maxlength: 50
            },
            BrandModel:
            {
                maxlength: 50
            },
            EngineId:
            {
                maxlength: 50
            },
            VIN:
            {
                maxlength: 50
            },
            seatsNum:
            {
                digits: true ,//整数
                min: 0,
                max:255
            },
            OilType:
            {
                maxlength: 50
            },
            PurchaseNum:
            {
                maxlength: 50
            },
            OperatNum:
            {
                maxlength: 50
            },
            
            EmptyCost:
            {
                number:true
            },
            tiresNum:
            {
                digits: true,//整数
                min: 0,
                max: 255
            },
            FullCost:
            {
                number: true
            },
            Tonnage:
            {
                number: true
            },
            LoadAmount:
            {
                number: true
            },
            FuelCapacity:
            {
                number: true
            },
            SalvageValue:
            {
                number: true
            },
            CarDealers:
            {
                maxlength: 50
            },
            LoadCapacity:
            {
                number: true
            },
            Fmemo:
            {
                maxlength: 255
            }
        }
        //onfocusout: function(element) { $(element).valid(); }
    });

    $("#submitBtn").click(function () {
        if (validator.form()) {
            $("#vehiclecreateform").submit();
        }
        else {
            artDialogAlert("输入不合法，请检查您的输入");
        }
    });

    //终端类型变化时，联动协议版本和记录仪版本
    $("#GPSType").change(function () {
        myajax("/Vehicle/GetGpsVersion",
                    $(this).val(),
                    function (data) {
                        if (data.success == false) {
                            artDialogAlert(data.message);
                            return;
                        }

                        $("#GPSVersion").children().remove();
                        var options = $.parseJSON(data.message);
                        var htmlStr;
                        $.each(options,function (index, Element) {
                            htmlStr += "<option value='" + options[index].Value + "'>" + options[index].Text + "</option>"
                        });
                        $("#GPSVersion").html(htmlStr);
                    },
                    function (data) {
                        //artDialogAlert("删除失败");
                        artDialogAlert(data.statusText);
                    },
                    "",
                    true
                );

        myajax("/Vehicle/GetTerminalVersion",
                    $(this).val(),
                    function (data) {
                        if (data.success == false) {
                            artDialogAlert(data.message);
                            return;
                        }

                        $("#TerminalVersion").children().remove();
                        var options = $.parseJSON(data.message);
                        var htmlStr;
                        $.each(options,function (index, Element) {
                            htmlStr += "<option value='" + options[index].Value + "'>" + options[index].Text + "</option>"
                        });
                        $("#TerminalVersion").html(htmlStr);
                    },
                    function (data) {
                        //artDialogAlert("删除失败");
                        artDialogAlert(data.statusText);
                    },
                    "",
                    true
                );
    });
});